//
//  BannerScrollView.m
//  翘脚老板
//
//  Created by 王涛 on 15/12/21.
//  Copyright © 2015年 bodecn. All rights reserved.
//

#import "BannerScrollViewTime.h"

@implementation BannerScrollViewTime

-(id)initWithFrame:(CGRect)frame with:(NSString *)imageKey{
    self = [super initWithFrame:frame];
    if (self) {
        _imageKey = imageKey;
    }
    return self;
}

-(void)setPicData:(NSArray *)arr{
    bannerArray = arr;
    _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
   
    _scroll.delegate = self;
    _scroll.pagingEnabled = YES;
    _scroll.showsVerticalScrollIndicator = NO;
    _scroll.showsHorizontalScrollIndicator = NO;
    [self addSubview:_scroll];
    pageControl = [[UIPageControl alloc] init];
    [self addSubview:pageControl];
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo (self.mas_right).offset (-26.0f);
        make.bottom.equalTo (self.mas_bottom);
    }];
    
    pageControl.currentPageIndicatorTintColor = RGBA(249, 83, 75, 1);
    pageControl.tintColor = RGBA(176, 176, 176, 1);
    
    imageArr = [[NSMutableArray alloc]init];
    if (bannerArray.count!=0) {
        imageArr = [[NSMutableArray alloc]init];
        _scroll.delegate = self;
        [bannerArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [imageArr addObject:obj];
        }];
        if (imageArr.count == 1) {
            _scroll.scrollEnabled = NO;
        }else{
            _scroll.scrollEnabled = YES;
        }
        [imageArr insertObject:[bannerArray objectAtIndex:bannerArray.count-1] atIndex:0];
        [imageArr insertObject:[bannerArray objectAtIndex:0] atIndex:imageArr.count];
        [_scroll setContentSize:CGSizeMake(_scroll.frame.size.width * [imageArr count], _scroll.frame.size.height)];
        
        [imageArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_scroll.frame.size.width*idx, 0, _scroll.frame.size.width-20 , _scroll.frame.size.height)];
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(_scroll.frame.size.width*idx +14, _scroll.frame.size.height - 40, _scroll.frame.size.width, 40)];
          
            
            lable.textColor = [UIColor whiteColor];
            
            lable.font = [UIFont boldSystemFontOfSize:18];
            if (_imageKey) {
                //后台有数据后 把注释放开,并把返回的相应的 key改正
                
                [imageView sd_setImageWithURL:[NSURL URLWithString:obj[@"pic_url"]] placeholderImage:[UIImage    imageNamed:@"占位图"]];
//                imageView.image = [UIImage imageNamed:@"ban"];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(turnActivityDetail:)];
                imageView.userInteractionEnabled = YES;
                [imageView addGestureRecognizer:tap];
             //   imageView.tag = [[obj objectForKey:@"GoodsInfoId"]integerValue];
                
                lable.text = obj[@"title"];
            }else{
                [imageView sd_setImageWithURL:[NSURL URLWithString:obj[@"pic_url"]] placeholderImage:[UIImage    imageNamed:@"占位图"]];
//                imageView.image = [UIImage imageNamed:@"ban"];
                lable.text = obj[@"title"];
            }
            //   NSLog(@"%@",obj[_imageKey]);
            [_scroll addSubview:imageView];
            [_scroll addSubview:lable];
            UILabel * label = [[UILabel alloc] init];
            [_scroll addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo (_scroll.mas_left).offset (14.0f);
                make.bottom.equalTo (_scroll.mas_bottom).offset(12.0f);
                make.height.equalTo (@(40.0f));
                make.width.equalTo (@(100));
            }];
            label.backgroundColor = [UIColor yellowColor];
            
        }];
        _scroll.contentOffset = CGPointMake(_scroll.frame.size.width, 0);
        pageControl.currentPage = 0;
        pageControl.numberOfPages = imageArr.count-2;
        if (!timer) {
            timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(scrollChange) userInfo:nil repeats:YES];
        }
    }
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [timer invalidate];
    timer = nil;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger offset = _scroll.contentOffset.x/ScreenWidth;
    if (offset == pageControl.numberOfPages+1) {
        pageControl.currentPage=0;
        [_scroll setContentOffset:CGPointMake(ScreenWidth, 0) animated:NO];
    }else if(offset == 0){
        pageControl.currentPage=imageArr.count-2;
        [_scroll setContentOffset:CGPointMake(ScreenWidth*(imageArr.count-2), 0) animated:NO];
    }else{
        pageControl.currentPage=(int)offset-1;
        pageNum = pageControl.currentPage;
    }
    //延迟4秒在开启
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(scrollChange) userInfo:nil repeats:YES];
}

-(void)scrollChange{
    if (bannerArray.count<=1) {
        [timer invalidate];
        return;
    }
    NSInteger offset = _scroll.contentOffset.x/ScreenWidth;
    if (offset == pageControl.numberOfPages) {
        [UIView animateWithDuration:1 animations:^{
            [_scroll setContentOffset:CGPointMake(_scroll.contentOffset.x+ScreenWidth, 0) animated:NO];
        } completion:^(BOOL finished){
            pageControl.currentPage=0;
            [_scroll setContentOffset:CGPointMake(ScreenWidth, 0) animated:NO];
        }];
    }else{
        [UIView animateWithDuration:1 animations:^{
            [_scroll setContentOffset:CGPointMake(_scroll.contentOffset.x+ScreenWidth, 0) animated:NO];
        } completion:^(BOOL finished){
            pageControl.currentPage=(int)offset;
            pageNum = pageControl.currentPage;
        }];
    }
}

-(void)closeTimer{
    [timer invalidate];
    timer = nil;
}

-(void)startTimer{
    if (!timer) {
        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scrollChange) userInfo:nil repeats:YES];
        //[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
    }
}

-(void)turnActivityDetail:(UITapGestureRecognizer *)tap{
    if ( _bannerClickBlock) {
        _bannerClickBlock(tap.view.tag);
    }
    

    
}

@end
